<?php


namespace backend\controllers;


use common\helpers\commonApi;
use common\helpers\DBManager;
use common\helpers\TransformApi;

class NewController extends BaseController
{
    public function actionMo()
    {
        $downUrlAll = [
            ['name'=>'api','value'=>'http://www.jokejie.top/template/api.zip'],
            ['name'=>'rbac后台','value'=>'http://www.jokejie.top/template/rbacbackend.zip'],
        ];
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $downUrl = $post['url'];
            $path = $post['path'];
            $template = $post['template'];
            $filename = basename($downUrl);
            $downRes = TransformApi::downloadFile($downUrl,$path.'/'.$filename);
            if (!$downRes){
                return commonApi::message('下载失败',false);
            }
            $res = TransformApi::uZip($path.'/'.$filename,$path.'/'.$post['name']);
            @unlink($path.'/'.$filename);
            if (!$res){
                return commonApi::message('解压失败',false);
            }
            TransformApi::fileReplace($path.'/'.$post['name'],[$template],[$post['name']]);
            //数据库添加
            require \Yii::$app->vendorPath.'/../common/config/database.php';
            if (empty($dbs)){
                $dbs = [];
            }
            if (!empty($dbs["app-{$post['name']}"])){
                @unlink($path.'/'.$post['name']);
                return commonApi::message('数据库配置已经存在',false);
            }
            $myDb = [
                "app-{$post['name']}"=>[
                    'class' => 'yii\db\Connection',
                    'dsn' => "mysql:host={$post['host']};port={$post['port']};dbname={$post['dbname']}",
                    'username' => $post['username'],
                    'password' => $post['password'],
                    'charset' => 'utf8',
                    'enableSchemaCache' => false,
                    'schemaCacheDuration' => 0,
                    'schemaCache' => 'cache',
                    'tablePrefix' => $post['tablePrefix'],
                ]
            ];
            $dbs = array_merge($dbs,$myDb);
            $text="<?php \n".'$dbs = '.stripslashes(var_export($dbs,true)).';'."\n".' return $dbs;';
            file_put_contents(\Yii::$app->vendorPath.'/../common/config/database.php',$text);//写入缓存
            if (file_exists($path.'/'.$post['name'].'/like.sql')){
                try {
                    $pdo = new DBManager($post['host'],$post['username'],$post['password'],$post['dbname']);
                    $pdo->tableExists();
                    $pdo->populate_db($post['tablePrefix'],$path.'/'.$post['name'].'/like.sql');
                    $pdo->initAccount($post['adminusername'],$post['adminpassword'],$post['tablePrefix']);
                    @unlink($path.'/'.$post['name'].'/like.sql');
                } catch (\Exception $e) {
                    return commonApi::message($e->getMessage(),false);
                }
            }
            return commonApi::message('成功');
        }
        $path = dirname(dirname(__DIR__));
        return $this->render('',['path'=>$path,'downUrlAll'=>$downUrlAll]);
    }
}